﻿/* cd /projects/programs/flin/0_master/ */
/* 

%Let dir_msas = /projects/programs/flin/0_master;
%include "&dir_msas/data_0_firm_local.sas";

%lbdid_firmnum_orig(c_city= );
%firmnum_new_lbdid(c_city= );
%firmnum_local(c_city= );
%merge_local(c_city= );
%aggregate_local(bi_var= , c_city= );

*/

/*=============================================================================*/
/* Find original owner of lbdid */
/*=============================================================================*/

%macro lbdid_firmnum_orig(c_city= );

/* The first year when an establishment is present in the country/MSA/geo */
proc sort data=lbd;
  by lbdid;
run;

proc means data=lbd noprint;
  by lbdid;
  var year;
  output out=lbd_fy1 min=lbd_y1;
run;

data lbd;
  merge lbd lbd_fy1;
  by lbdid;
run;

/* Retrieve the original owner of the establishment */
data lbd_fy1;
  set lbd;
  if year = lbd_y1;
  keep lbdid firmnum;
run;

data lbd_fy1;
  set lbd_fy1;
  rename firmnum=firmnum_orig;
run;

data lbd;
  merge lbd lbd_fy1;
  by lbdid;
run;

%mend;


/*=============================================================================*/
/* Find first new establishment of a firm */
/*=============================================================================*/

%macro firmnum_new_lbdid(c_city= );

/* The first year when a firm is present at all in the country */
proc sort data=lbd;
  by firmnum year;
run;

proc means data=lbd noprint;
  by firmnum;
  var year;
  output out=lbd_fy1 min=firm_y1_r;
run;

data lbd;
  merge lbd lbd_fy1;
  by firmnum;
run;

/* The first year when a firm creates its first new establishment in the country */
data lbd_fy1;
  set lbd;
  if year = lbd_y1;
run;

proc sort data=lbd_fy1;
  by firmnum year;
run;

proc means data=lbd_fy1 noprint;
  by firmnum;
  var year;
  output out=lbd_fy1 min=firm_y1;
run;

data lbd;
  merge lbd lbd_fy1;
  by firmnum;
run;


/* The first establishment created by a firm */
data lbd;
  set lbd;
  if firm_y1 = lbd_y1 then firm_estzero = 1;
run;

%mend;


/*=============================================================================*/
/* Defining local or foreign firm based on first establishments */
/* Defining local or foreign establishment based on original owner */
/*=============================================================================*/

%macro firmnum_local(c_city= );

data lbd_lcl;
  set lbd;
  if year = lbd_y1; /* Keep only new establishments */
  if year = firm_y1; /* Keep only years when firms first create new establishments */
run;

proc sort data=lbd_lcl;
  by year firmnum &c_city.;
run;

proc means data=lbd_lcl noprint;
  by year firmnum &c_city.;
  output out=lbd_lcl sum(worker)=worker_lcl; 
run;


/* Create random variable used to break ties for firms with same workerloyment in more than 1 MSA */
proc sort data=lbd_lcl;
  by year firmnum worker_lcl &c_city.;
run;

/*
data lbd_lcln;
  set lbd_lcl;
  n_&c_city. = 1;
run;

proc means data=lbd_lcln noprint;
  by year firmnum worker_lcl;
  output out=lbd_lcln sum(n_&c_city.)=n_&c_city.;
run;

data lbd_lcl;
  merge lbd_lcl lbd_lcln;
  by year firmnum worker_lcl;
run;

data lbd_lcl;
  set lbd_lcl;
  call streaminit(5807);
  if n_&c_city. > 1 then tie_rand =rand("Uniform");
run;
*/

data lbd_lcl;
  set lbd_lcl;
  call streaminit(5807);
  tie_rand =rand("Uniform");
run;

proc sort data=lbd_lcl;
  by year firmnum worker_lcl tie_rand;
run;

/* Keep only the firm-MSA with the most workerloyment */
data lbd_lcl;
  set lbd_lcl;
  by year firmnum;
  if Last.firmnum;
  firm_local = 1;
  keep firmnum &c_city. firm_local;
  rename firmnum=firmnum_orig;
run;

proc sort data=lbd_lcl;
  by firmnum_orig &c_city.;
run;

%mend;


/*=============================================================================*/
/* Merge definition of local with lbd */
/*=============================================================================*/

%macro merge_local(c_city= );

proc sort data=lbd_lcl;
  by firmnum_orig &c_city.;
run;

proc sort data=lbd;
  by firmnum_orig &c_city.;
run;

data lbd;
  merge lbd(in=in_main) lbd_lcl(in=in_lcl);
  by firmnum_orig &c_city.;
  if in_main=1;
run;

data lbd;
  set lbd;
  if firm_local = . then firm_local = 0;
  rename firmnum=firmnum_real;
run;

%mend;


/*=============================================================================*/
/* Aggregate to firm level */
/*=============================================================================*/

%macro aggregate_local(by_var= , c_city= );

proc sort data=lbd;
  by year &c_city. firmnum_orig firm_local &by_var.;
run;

proc means data=lbd noprint;
  by year &c_city. firmnum_orig firm_local &by_var.;
  output out=lbd sum(worker est salary)=worker est salary;
run;

%mend;

